package cn.wolfcode.luowowo.scenic.service.impl;

import cn.wolfcode.luowowo.common.query.ScenicDetailQueryObject;
import cn.wolfcode.luowowo.scenic.domain.ScenicComment;
import cn.wolfcode.luowowo.scenic.domain.ScenicDetail;
import cn.wolfcode.luowowo.scenic.mapper.ScenicCommentMapper;
import cn.wolfcode.luowowo.scenic.mapper.ScenicDetailMapper;
import cn.wolfcode.luowowo.scenic.service.IScenicCommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ScenicCommentServiceImpl implements IScenicCommentService {
    @Autowired
    private ScenicCommentMapper scenicCommentMapper;
    @Autowired
    private ScenicDetailMapper detailMapper;

    @Override
    public List<ScenicComment> list() {
        List<ScenicComment> scenicComments = scenicCommentMapper.selectAll();
        return scenicComments;
    }

    @Override
    public List<ScenicComment> getByUserId(Long userId, Long type) {
        List<ScenicComment> list = scenicCommentMapper.selectByUserId(userId, type);
        for (ScenicComment scenicComment : list) {
            Long scenicId = scenicComment.getScenicId();
            ScenicDetail scenicDetail = detailMapper.selectByPrimaryKey(scenicId);
            scenicComment.setScenicName(scenicDetail.getName());
            String[] coverUrls = scenicDetail.getCoverUrls();
            if (coverUrls != null && coverUrls.length > 0) {
                scenicComment.setScenicImg(coverUrls[0]);
            }
            scenicComment.setDestId(scenicDetail.getDestId());
        }
        return list;
    }

    @Override
    public void updateStats(ScenicComment scenicComment) {
        scenicCommentMapper.updateStats(scenicComment);
    }

    @Override
    public int getTotalReview(Long userId, Long type) {
        return scenicCommentMapper.selectByUserId(userId, type).size();
    }

    @Override
    public PageInfo<ScenicComment> query(ScenicDetailQueryObject qo) {
        List<ScenicComment> list = new ArrayList<>();
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        switch (qo.getCategory()) {
            case ScenicDetailQueryObject.HAS_PICTURE:
                qo.setCondition("sc.imgUrls is not null and sc.imgUrls!=''");
                break;
            case ScenicDetailQueryObject.GOLD_COMMENT:
                qo.setCondition("sc.type= 1 ");
                break;
            case ScenicDetailQueryObject.GOOD_COMMENT:
                qo.setCondition("sc.rank > 3 ");
                break;
            case ScenicDetailQueryObject.NORMAL_COMMENT:
                qo.setCondition("sc.rank = 3 ");
                break;
            case ScenicDetailQueryObject.BAD_COMMENT:
                qo.setCondition("sc.rank < 3 ");
                break;
        }
        list = scenicCommentMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    public ScenicComment getById(Long commentId) {
        ScenicComment scenicComment = scenicCommentMapper.selectByPrimaryKey(commentId);
        return scenicComment;
    }


    public void saveOrUpdate(ScenicComment scenicComment) {

        if (scenicComment.getId() != null) {
            scenicComment.setCreateTime(new Date());
            scenicCommentMapper.updateByPrimaryKey(scenicComment);
        } else {
            scenicCommentMapper.insert(scenicComment);
        }

    }
}
